package com.example.controller;

import com.example.entity.TShopOrder;
import com.example.service.ITShopOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author llp
 * @since 2025-10-14
 */
@RestController
public class TShopOrderController {
    @Autowired
    private ITShopOrderService tShopOrderService;

    @GetMapping("/save/{pid}/{uid}/{number}")
    public TShopOrder save(@PathVariable Long pid,@PathVariable Long uid,@PathVariable Integer number){
        TShopOrder tShopOrder = new TShopOrder();
        tShopOrder.setPid(pid);

//        List<String> p1 = new ArrayList<>();
//        p1.add("localhost");
//        p1.add("localhost");
//        p1.add("localhost");
//
//        List<Integer> p2 = new ArrayList<>();
//        p2.add(8081);
//        p2.add(8082);
//        p2.add(8083);
//
//        Random random = new Random();
//        int i = random.nextInt(p1.size());
//
//        RestTemplate restTemplate = new RestTemplate();
//        TShopOrder product = restTemplate.getForObject("http://"+p1.get(i)+":"+p2.get(i)+"/get/" + pid, TShopOrder.class);
//        tShopOrder.setPname(product.getPname());
//        tShopOrder.setPprice(product.getPprice());

        RestTemplate restTemplate = new RestTemplate();
        TShopOrder forObject = restTemplate.getForObject("http://localhost:8081/get/" + pid, TShopOrder.class);
        tShopOrder.setPname(forObject.getPname());
        tShopOrder.setPprice(forObject.getPprice());


        tShopOrder.setUid(uid);
        tShopOrder.setNumber(number);
        tShopOrderService.save(tShopOrder);
        return tShopOrder;
    }



}
